package org.budo.jdbc.dao.statement.named.parser;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.budo.jdbc.dao.statement.named.NamedParameterQuery;

/**
 * @author lmw
 */
public class CachedNamedParameterQueryParser implements NamedParameterQueryParser {
    private NamedParameterQueryParser queryParser;

    private Map<String, NamedParameterQuery> cacheMap = new ConcurrentHashMap<String, NamedParameterQuery>();

    public CachedNamedParameterQueryParser(NamedParameterQueryParser queryParser) {
        this.queryParser = queryParser;
    }

    @Override
    public NamedParameterQuery parseQuery(String sql) {
        if (null == sql) {
            return null;
        }

        NamedParameterQuery query = cacheMap.get(sql);
        if (null != query) {
            return query;
        }

        NamedParameterQuery parseQuery = queryParser.parseQuery(sql);
        cacheMap.put(sql, parseQuery);
        return parseQuery;
    }
}